﻿using FluentValidation;
using Mes.Application.Common;
using Mes.Application.DTO.Inputs.Quality;
using Mes.Application.DTO.Results.Quality;
using Mes.Application.Services.Quality;
using Mes.Domain.BoundedContext;
using Microsoft.AspNetCore.Mvc;

namespace Mes.Application.Web.Controllers.Quality
{
    /// <summary>
    /// 质量特征控制器
    /// </summary>
    [ApiController]
    [Route("/api/v1")]
    public class CharacteristicController : ControllerBase
    {
        private readonly ICharacteristicService _characteristicService;
        private readonly ILogger<CharacteristicController> _logger;
        private readonly IValidator<AddingCharacteristicInput> _addingCharacteristicInputValidator;
        private readonly IValidator<UpdatingCharacteristicInput> _updatingCharacteristicInputValidator;
        private readonly IValidator<GettingCharacteristicsInput> _gettingCharacteristicInputValidator;

        /// <summary>
        /// 质量特征控制器
        /// </summary>
        /// <param name="characteristicService">质量特征服务</param>
        /// <param name="logger">日志服务</param>
        /// <param name="addingCharacteristicInputValidator">添加质量特征验证器</param>
        /// <param name="updatingCharacteristicInputValidator">编辑质量特征验证器</param>
        /// <param name="gettingCharacteristicInputValidator">查询质量特征验证器</param>
        public CharacteristicController(ICharacteristicService characteristicService, ILogger<CharacteristicController> logger, IValidator<AddingCharacteristicInput> addingCharacteristicInputValidator, IValidator<UpdatingCharacteristicInput> updatingCharacteristicInputValidator, IValidator<GettingCharacteristicsInput> gettingCharacteristicInputValidator)
        {
            _characteristicService = characteristicService;
            _logger = logger;
            _addingCharacteristicInputValidator = addingCharacteristicInputValidator;
            _updatingCharacteristicInputValidator = updatingCharacteristicInputValidator;
            _gettingCharacteristicInputValidator = gettingCharacteristicInputValidator;
        }

        /// <summary>
        /// 新增质量特征
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("char")]
        public async Task<ActionResult> AddCharacteristicAsync([FromBody] AddingCharacteristicInput input)
        {
            try
            {
                var result = await _addingCharacteristicInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                await _characteristicService.AddCharacteristicAsync(input);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message, ex);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除质量特征
        /// </summary>
        /// <param name="char_id">质量特征Id</param>
        /// <returns></returns>
        [HttpDelete("char")]
        public async Task<ActionResult> DeleteCharacteristicAsync(int char_id)
        {
            try
            {
                await _characteristicService.DeleteCharacteristicAsync(char_id);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message, ex);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 编辑质量特征
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPatch("char")]
        public async Task<ActionResult> UpdateCharacteristicAsync([FromBody] UpdatingCharacteristicInput input)
        {
            try
            {
                var result = await _updatingCharacteristicInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                await _characteristicService.UpdateCharacteristicAsync(input);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message, ex);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询质量特征实体
        /// </summary>
        /// <param name="char_id">质量特征Id</param>
        /// <returns></returns>
        [HttpGet("char")]
        public async Task<ActionResult<CharacteristicDetailResult>> GetCharacteristicAsync(int char_id)
        {
            try
            {

                var response = await _characteristicService.GetCharacteristicAsync(char_id);
                return Ok(response);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message, ex);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询质量特征分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("chars")]
        public async Task<ActionResult<PagedItemsData<CharacteristicResult>>> GetCharacteristicsAsync([FromQuery] GettingCharacteristicsInput input)
        {
            try
            {
                var result = await _gettingCharacteristicInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                var response = await _characteristicService.GetCharacteristicsAsync(input);
                return Ok(response);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message, ex);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 根据质量规格ID列表获取关联的质量特征
        /// </summary>
        /// <param name="qm_spec_id_list">质量规则ID列表，以，分隔</param>
        /// <returns></returns>
        [HttpGet("spec/chars")]
        public async Task<ActionResult<ItemsResult<CharacteristicResultForQmSpec>>> GetCharacteristicForQmSpecAsync([FromQuery] string? qm_spec_id_list)
        {
            try
            {
                var response = await _characteristicService.GetCharacteristicForQmSpecAsync(qm_spec_id_list);
                return Ok(response);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message, ex);
                return BadRequest(ex.Message);
            }
        }
    }
}
